package com.topway.dsmflex.thread.job;

import com.topway.dsmflex.Log;
import com.topway.dsmflex.model.base.DataMonitorBean;
import com.topway.dsmflex.model.synchro.DataTableBean;
import com.topway.dsmflex.thread.services.DbSynchroServices;
import com.topway.dsmflex.thread.services.SynchroServices;


/**
 * 单个表同步线程
 * 
 * @author liusy 2013-05-09
 *
 */
public class DbSynchroThread extends AbstractDataThread{

	private DataTableBean dt = null;
	private DbSynchroServices synchroServices = null;

	public DbSynchroThread(SynchroManager sm,DataTableBean dt)
	{
		super(sm,dt);
		this.dt = dt;
		synchroServices =new DbSynchroServices();
	}
	
	@Override
	public int SynchroData()
	{
		int _result=0;
		try {
			Log.info("正在同步表["+dt.getName()+"]");
			dt.setPageIndex(0);
			if(dt.getOperatorTarget().equals("UPDATE"))
				_result = synchroServices.synchroUpdateData(dt);
			else if(dt.getOperatorTarget().equals("INSERT"))
				_result = synchroServices.synchroInsertData(dt);
			else if(dt.getOperatorTarget().equals("INSERTUPDATE"))
				_result = synchroServices.synchroInsertOrUpdateData(dt);
			else if(dt.getOperatorTarget().equals("DELETE"))
				_result = synchroServices.synchroDeleteData(dt);
			_result =(((dt.getPageIndex()-2)*dt.getPageSize())+_result);
			dt.propertyChange(DataMonitorBean.SIZE,"",""+(_result<0?0:_result));
			Log.info("源表["+dt.getName()+"] 对目标表   ["+dt.getTableNameTarget()+"]  同步  ["+dt.getOperatorTarget()+"]完成，已同步["+(_result<0?0:_result)+"]条数据!");
		} catch (Exception e) {
			Log.error("同步表["+dt.getName()+"]发生错误，忽略!"+e.getMessage());
		}
		return _result;
	}
	
	
	
	public static void main(String[] args) {
		 
		String nterval  =String.valueOf((1000*60*0.5));
			System.out.println(nterval.indexOf(".")==-1?Long.parseLong(nterval):Long.parseLong(nterval.substring(0, nterval.indexOf("."))));
	}
	
	
}
